<template>
  <page-wrap back hide-tab title="常用语编辑" stay-still>
    <view class="edit">
      <my-textarea
        class="content"
        :value.sync="value"
        placeholder="请输入你的常用语，注意避免使用敏感词汇及信息"
        :maxlength="200"
        auto-height
        show-word-limit
        lengthColor="#F75A3E"
      ></my-textarea>
    </view>
    <view class="fixed-bottom">
      <view class="btn btn-primary btn-add" @click="submit">
        完成
      </view>
    </view>
  </page-wrap>
</template>

<script>
import MyTextarea from "../components/MyTextarea.vue";
import { commonTermsDetail, addCommonTerms, updataCommonTerms } from '@/api/modules/message';

export default {
  components: {
    MyTextarea,
  },
  data() {
    return {
      personId: uni.getStorageSync("person-id").slice(2) || '',
      loginStatus: uni.getStorageSync("login-status") || "person",
      id: '',
      value: '',
      laoding: false,
    }
  },
  onLoad(opt) {
    this.id = opt.id || '';
  },
  mounted() {
    this.id && this.getData();
  },
  methods: {
    getData() {
      const params = {
        commonLanguageId: this.id,
      };
      commonTermsDetail(params).then(res => {
        if(res.code == 200) {
          this.value = res.data.commonLanguage;
        } else {
          uni.showToast({
            title: res.status_desc,
            icon: 'none',
            duration: 1000
          });
        }
      })
    },
    submit() {
      if(!this.value) {
        return uni.showToast({
          title: '请输入文字',
          icon: 'none',
          duration: 1000
        });
      }

      if(this.laoding) return;

      let params = {
        personId: this.personId,
        commonLanguage: this.value,
      };

      if(this.id) {
        params.commonLanguageId = this.id;
      } else {
        params.type = this.loginStatus == 'person'? 1: 2;
      }
  
      this.laoding = true;
      uni.showLoading();
      let fn = addCommonTerms;
      if(this.id) {
        fn = updataCommonTerms;
      }
      fn(params).then(res => {
        if(res.code == 200) {
          uni.showToast({
            title: '保存成功',
            icon: 'none',
          });
          setTimeout(()=> {
            uni.navigateBack();
          }, 1000);
          this.$bus.$emit('updataCommonTerms');
        } else {
          uni.showToast({
            title: res.status_desc,
            icon: 'none',
            duration: 1000
          });
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.edit {
  padding: 32rpx;

  ::v-deep .content {
    display: block;
    box-sizing: border-box;
    .textarea-input {
      border: 2rpx solid #EDEDF1;
      border-radius: $uni-border-radius-big;
      textarea,
      .textarea {
        height: 500rpx !important;
        font-size: $uni-font-size-sm;
      }
      .placeholder {
        font-size: $uni-font-size-sm;
      }
    }
  }
}
.fixed-bottom {
  position: fixed;
  bottom: constant(safe-area-inset-bottom);
  bottom: env(safe-area-inset-bottom);
  width: 100%;
  padding: 24rpx 32rpx;
  z-index: 2;

}
</style>